package com.clayecloud.module_report_repair.views;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.clayecloud.lib_base.action.ToastAction;
import com.clayecloud.lib_base.dialog.InputDialog;
import com.clayecloud.lib_base.utils.KeyboradUtil;
import com.clayecloud.module_report_repair.R;
import com.clayecloud.module_report_repair.databinding.ActivityReportRepairMainBinding;
import com.clayecloud.module_report_repair.viewModels.ReportRepairMainViewModel;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.bar.OnTitleBarListener;

@Route(path = "/repair/index")
public class ReportRepairMainActivity extends AppCompatActivity implements ToastAction {

    private ActivityReportRepairMainBinding reportRepairMainBinding;

    private ReportRepairMainViewModel reportRepairMainViewModel;

    private Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        reportRepairMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_report_repair_main);
        ViewModelProvider.Factory factory = ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication());
        ViewModelProvider viewModelProvider = new ViewModelProvider(this,factory);
        reportRepairMainViewModel = viewModelProvider.get(ReportRepairMainViewModel.class);
        // 初始化函数
        init();
        // 绑定监听事件
        initListener();
    }

    /**
     * 进行初始化
     */
    private void init() {
        // 控制状态栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        reportRepairMainViewModel.leadData();
    }

    /**
     * 注册监听事件
     */
    private void initListener() {
        reportRepairMainBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View view) {
                finish();
                overridePendingTransition(R.anim.window_fake, R.anim.window_right_out);
            }

            @Override
            public void onTitleClick(View view) {

            }

            @Override
            public void onRightClick(View view) {
                gotoRepairList();
            }
        });

        reportRepairMainBinding.repair.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reportRepairMainViewModel.setRrtype(0);
                reportRepairMainBinding.repair.setChecked(true);
                reportRepairMainBinding.report.setChecked(false);
            }
        });

        reportRepairMainBinding.report.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reportRepairMainViewModel.setRrtype(1);
                reportRepairMainBinding.repair.setChecked(false);
                reportRepairMainBinding.report.setChecked(true);
            }
        });

        reportRepairMainBinding.address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new InputDialog.Builder(context)
                        // 标题可以不用填写
                        .setTitle("地点")
                        .setHint(reportRepairMainBinding.address.getRightText())
                        .setListener((dialog, content) -> {
                            if (!reportRepairMainBinding.address.getRightText().equals(content)) {
                                reportRepairMainBinding.address.setRightText(content);
                                reportRepairMainBinding.address.setRightTextColor(Color.parseColor("#303133"));
                            }
                        })
                        .show();
            }
        });

        reportRepairMainBinding.username.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new InputDialog.Builder(context)
                        // 标题可以不用填写
                        .setTitle("联系人")
                        .setHint(reportRepairMainBinding.username.getRightText())
                        .setListener((dialog, content) -> {
                            if (!reportRepairMainBinding.username.getRightText().equals(content)) {
                                reportRepairMainBinding.username.setRightText(content);
                                reportRepairMainBinding.username.setRightTextColor(Color.parseColor("#303133"));
                            }
                        })
                        .show();
            }
        });

        reportRepairMainBinding.phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new InputDialog.Builder(context)
                        // 标题可以不用填写
                        .setTitle("电话")
                        .setHint(reportRepairMainBinding.phone.getRightText())
                        .setListener((dialog, content) -> {
                            if (!reportRepairMainBinding.phone.getRightText().equals(content)) {
                                reportRepairMainBinding.phone.setRightText(content);
                                reportRepairMainBinding.phone.setRightTextColor(Color.parseColor("#303133"));
                            }
                        })
                        .show();
            }
        });

        reportRepairMainBinding.content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                reportRepairMainBinding.contentCount.setText(Integer.toString(reportRepairMainBinding.content.getText().length()));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        reportRepairMainBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reportRepairMainViewModel.submit(reportRepairMainBinding.address.getRightText().toString(), reportRepairMainBinding.username.getRightText().toString(),
                        reportRepairMainBinding.phone.getRightText().toString(), reportRepairMainBinding.content.getText().toString());
            }
        });

        reportRepairMainViewModel.getRrViewState().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(aBoolean){
                    reportRepairMainBinding.address.setRightText("");
                    reportRepairMainBinding.username.setRightText("");
                    reportRepairMainBinding.phone.setRightText("");
                    reportRepairMainBinding.content.setText("");
                    gotoRepairList();
                }
            }
        });

    }

    /**
     * 跳转到报事报修列表页
     */
    private void gotoRepairList(){
        ARouter.getInstance()
                .build("/repair/list")
                .withTransition(R.anim.window_right_in, R.anim.window_fake)
                .navigation(this);
    }

    /**
     * 重写每次点击事件，并进行判断是不是editText
     * @param motionEvent
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (KeyboradUtil.isShouldHideInput(v, motionEvent)) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(motionEvent);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(motionEvent)) {
            return true;
        }
        return onTouchEvent(motionEvent);
    }

}